package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.common.TypeDictionaryTestDataDto;
import com.sg.dto.base.framework.req.*;
import com.sg.entity.TypeDictionary;
import com.sg.service.base.framework.MTypeDictionaryService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.TypeDictionaryMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MTypeDictionaryServiceImpl
        extends ServiceImpl<TypeDictionaryMapper, TypeDictionary>
        implements MTypeDictionaryService {

    @Autowired
    TypeDictionaryMapper typeDictionaryMapper;

    /**
     * 1-3-16查询组织架构类型字典详情[950]
     * gen by moon at 10/16/2022, 11:08:56 AM
     */
    @Trace(operationName = "1-3-16查询组织架构类型字典详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public TypeDictionary queryOrgClassDetail(QueryOrgClassDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<TypeDictionary> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<TypeDictionary> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                TypeDictionary retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-16查询组织架构类型字典列表[295]
     * gen by moon at 12/10/2024, 12:03:51 AM
     */
    @Trace(operationName = "1-3-16查询组织架构类型字典列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<TypeDictionary> queryOrgClassList(QueryOrgClassListReq reqDto) {

        try {
            QueryWrapper<TypeDictionary> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(TypeDictionary.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(TypeDictionary.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(TypeDictionary.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<TypeDictionary>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-16批量查询组织架构类型字典列表	[1150]
     * gen by moon at 10/16/2022, 11:08:57 AM
     */
    @Trace(operationName = "1-3-16批量查询组织架构类型字典列表	")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<TypeDictionary> batchQueryOrgClassList(BatchQueryOrgClassListReq reqDto) {

        try {
            QueryWrapper<TypeDictionary> queryWrapper = new QueryWrapper<>();

            if (reqDto.getOrgClassList() == null || reqDto.getOrgClassList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("type_dictionary_id", reqDto.getOrgClassList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(TypeDictionary.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(TypeDictionary.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(TypeDictionary.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<TypeDictionary>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-16修改组织架构类型字典[292]
     * gen by moon at 10/16/2022, 11:08:54 AM
     */
    @Trace(operationName = "1-3-16修改组织架构类型字典")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateOrgClass(TypeDictionary typeDictionary) {
        typeDictionary.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (typeDictionary != null && typeDictionary.getSubjectLifeCycle() != null && typeDictionary.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            typeDictionary.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (typeDictionary != null && typeDictionary.getSubjectLifeCycle() != null && typeDictionary.getSubjectLifeCycle().equals("ARCHIVING")) {
            typeDictionary.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        typeDictionary.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (typeDictionary != null && typeDictionary.getSubjectLifeCycle() != null && typeDictionary.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            typeDictionary.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (typeDictionary != null && typeDictionary.getSubjectLifeCycle() != null && typeDictionary.getSubjectLifeCycle().equals("ARCHIVING")) {
            typeDictionary.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(typeDictionary);
    }

    /**
     * 1-3-16新增组织架构类型字典[291]
     * gen by moon at 10/16/2022, 11:08:54 AM
     */
    @Trace(operationName = "1-3-16新增组织架构类型字典")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addOrgClass(TypeDictionary typeDictionary) {
        typeDictionary.setTypeDictionaryId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        typeDictionary.setIsArchive("FALSE");
        typeDictionary.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        typeDictionary.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (typeDictionary != null && typeDictionary.getSubjectLifeCycle() != null && typeDictionary.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            typeDictionary.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        typeDictionary.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        typeDictionary.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (typeDictionary != null && typeDictionary.getSubjectLifeCycle() != null && typeDictionary.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            typeDictionary.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        typeDictionary.setIsValid("FALSE");
        typeDictionary.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        typeDictionary.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(typeDictionary)) {
            return typeDictionary.getTypeDictionaryId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-3-16删除组织架构类型字典[293]
     * gen by moon at 10/16/2022, 11:08:55 AM
     */
    @Trace(operationName = "1-3-16删除组织架构类型字典")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteOrgClass(String typeDictionaryId) {

        return super.removeById(typeDictionaryId);
    }

    /**
     * M1查询组织架构类型字典测试列表[4895]
     * gen by moon at 10/16/2022, 11:08:57 AM
     */
    @Trace(operationName = "M1查询组织架构类型字典测试列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<TypeDictionary> queryTypeDictionaryTestDataList(TypeDictionary typeDictionary) {

        try {
            QueryWrapper<TypeDictionary> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(typeDictionary);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(TypeDictionary.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(TypeDictionary.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(TypeDictionary.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<TypeDictionary>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M1批量删组织架构类型字典测试[4896]
     * gen by moon at 10/16/2022, 11:08:58 AM
     */
    @Trace(operationName = "M1批量删组织架构类型字典测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteTypeDictionaryTestData(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 1-3-16执行主题标识模糊查类型字典[9090]
     * gen by moon at 4/2/2024, 11:30:16 PM
     */
    @Trace(operationName = "1-3-16执行主题标识模糊查类型字典")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<TypeDictionary> implementThemeCodeVagueQueryOrgClass(ImplementThemeCodeVagueQueryOrgClassReq reqDto) {

        try {
            QueryWrapper<TypeDictionary> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(TypeDictionary.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(TypeDictionary.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(TypeDictionary.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<TypeDictionary>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-16查询工作任务来源详情[9120]
     * gen by moon at 4/2/2024, 11:30:26 PM
     */
    @Trace(operationName = "1-3-16查询工作任务来源详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public TypeDictionary queryWorkTaskSourceDetail(QueryWorkTaskSourceDetailReq reqDto) {

        try {

            QueryWrapper<TypeDictionary> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<TypeDictionary> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                TypeDictionary retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M1执行接收工作任务来源[9109]
     * gen by moon at 4/3/2024, 12:09:19 AM
     */
    @Trace(operationName = "M1执行接收工作任务来源")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<TypeDictionary> implementReceiveWorkTaskSource(ImplementReceiveWorkTaskSourceReq reqDto) {

        try {
            QueryWrapper<TypeDictionary> queryWrapper = new QueryWrapper<>();

            queryWrapper.in("type_dictionary_code", reqDto.getTypeDictionaryTestDataList().stream().map(TypeDictionaryTestDataDto::getTypeDictionaryCode).collect(Collectors.toList()));
            queryWrapper.in("type_dictionary_name", reqDto.getTypeDictionaryTestDataList().stream().map(TypeDictionaryTestDataDto::getTypeDictionaryName).collect(Collectors.toList()));
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(TypeDictionary.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(TypeDictionary.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(TypeDictionary.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<TypeDictionary>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-16查询任务分类列表[10041]
     * gen by moon at 7/24/2024, 2:46:15 AM
     */
    @Trace(operationName = "1-3-16查询任务分类列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<TypeDictionary> queryTaskCategoriesList(QueryTaskCategoriesListReq reqDto) {

        try {
            QueryWrapper<TypeDictionary> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(TypeDictionary.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(TypeDictionary.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(TypeDictionary.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<TypeDictionary>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
